package org.teamh.gui.lobby;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.teamh.logic.lobby.Game;

public class GameBoxController {
	
	private LobbyController lobbyController;
	private List<Game> oldList;

	public GameBoxController(LobbyController lobbyController) {
		this.lobbyController = lobbyController;
	}

	public void checkForUpdateGameList(LinkedList<Game> newGameList) {
		if (oldList == null) {
			oldList = new ArrayList<Game>();
		}
		boolean isIn = false;
		int listIndex = -1;
		int gameBoxIndex = -1;
		
		for (int i = 0; i < newGameList.size(); i++) {
			for (int j = 0; j < oldList.size(); j++) {
				if (oldList.get(j).getName()
						.equals(newGameList.get(i).getName())) {
					listIndex = j;
					gameBoxIndex = lobbyController.getGameBox().getItems().indexOf(findGameEntryModel(oldList.get(j)));
					if(oldList.get(j).getPlayers()
							.equals(newGameList.get(i).getPlayers())) {
						if(oldList.get(j).getStatus()
								.equals(newGameList.get(i).getStatus())) {
							isIn = true;
						}
					}
				}
			}
			if (!isIn) {
				if(listIndex > -1) {
					oldList.remove(listIndex);
					oldList.add(newGameList.get(i));
					updateGameBox(newGameList.get(i), gameBoxIndex);
				} else {
					oldList.add(newGameList.get(i));
					addToGameBox(newGameList.get(i));
				}
			}
			listIndex = -1;
			gameBoxIndex = -1;
			isIn = false;
		}
		for (int i = 0; i < oldList.size(); i++) {
			for (int j = 0; j < newGameList.size(); j++) {
				if (newGameList.get(j).getName()
						.equals(oldList.get(i).getName())) {
					if(newGameList.get(j).getPlayers()
							.equals(oldList.get(i).getPlayers())) {
						if(newGameList.get(j).getStatus()
								.equals(oldList.get(i).getStatus())) {
							isIn = true;
						}
					}
				}
			}
			if (!isIn) {
				deleteFromGameBox(oldList.get(i));
				oldList.remove(oldList.get(i));
			}
			isIn = false;
		}
	}

	private void updateGameBox(Game game, int gameBoxIndex) {
		boolean selected = false;
		boolean focused = false;
	
		if(lobbyController.getGameBox().getSelection().size() > 0 
				&& lobbyController.getGameBox().getSelection().get(0).getName().equals(game.getName())) {
			selected = true;
		}
		if(lobbyController.getGameBox().getFocusItem().getName().equals(game.getName())) {
			focused = true;
		}
			
		lobbyController.getGameBox().removeItemByIndex(gameBoxIndex);
		
		GameEntryModel g = new GameEntryModel(game.getName(), game.getMap(), game.getPlayers(), game.getStatus());
		lobbyController.getGameBox().insertItem(g, gameBoxIndex);
		
		if(selected) {
			lobbyController.getGameBox().selectItem(g);
		}
		if(focused) {
			lobbyController.getGameBox().setFocusItem(g);
		}
	}
	
	private void deleteFromGameBox(Game game) {
		lobbyController.getGameBox().removeItem(findGameEntryModel(game));		
	}

	private GameEntryModel findGameEntryModel(Game game) {
		List<GameEntryModel> games = lobbyController.getGameBox().getItems();
		GameEntryModel model = null;
		for(GameEntryModel g : games) {
			if(g.getName().equals(game.getName())) {
				model = g;
			}
		}
		return model;
	}
	
	private void addToGameBox(Game game) {
		lobbyController.getGameBox().addItem(new GameEntryModel(game.getName(), game.getMap(), game.getPlayers(), game.getStatus()));		
	}
	
	public void deleteAll() {
	}
}
